package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.util.IdWorker;
import com.contest.util.MD5Utils;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.contest.entity.MerchantInfo;
import com.contest.service.MerchantInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 
 *
 * @author liuyf
 * @email 123456@qq.com
 * @date 2018-01-19 14:16:33
 */

@Controller
@RequestMapping("/merchantInfo")
public class MerchantInfoController {

    @Autowired
    MerchantInfoService merchantInfoService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param,HttpServletRequest request) {
        MerchantInfo merchantInfo = (MerchantInfo) request.getSession().getAttribute("user");
        Wrapper<MerchantInfo> w = new EntityWrapper<>();
        w.addFilter("fd_name={0}",merchantInfo.getFdName());
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<MerchantInfo> mPage = new Page<MerchantInfo>(page, rows);
        mPage = merchantInfoService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(MerchantInfo merchantInfo) {
        merchantInfo.setFdId(idWorker.nextId() + "");
        if (merchantInfoService.insert(merchantInfo)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        MerchantInfo merchantInfo = merchantInfoService.selectById(fdId);
        model.addAttribute("merchantInfo", merchantInfo);
        return "merchantInfo/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (merchantInfoService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(MerchantInfo merchantInfo) {
        if (merchantInfoService.updateById(merchantInfo)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

    @RequestMapping("/login")
    @ResponseBody
    public R login(String loginName, String loginPwd, HttpServletRequest request){
        Wrapper<MerchantInfo> w = new EntityWrapper<>();
        w.addFilter("fd_login_name = {0} and fd_login_pwd = {1}",loginName, MD5Utils.encrypt(loginPwd));
        MerchantInfo mi =  merchantInfoService.selectOne(w);
        if(mi == null){
            return R.error("账号或密码错误！");
        }else{
            request.getSession().setAttribute("user",mi);
            return R.ok();
        }
    }


    @RequestMapping("/loginOut")
    @ResponseBody
    public R loginOut(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        try {
            session.removeAttribute("user");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

}
